package cn.app.android.sichuanxiaoke.com.xiaokeapp.activity;

import android.content.Intent;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.app.android.sichuanxiaoke.com.xiaokeapp.R;
import cn.app.android.sichuanxiaoke.com.xiaokeapp.utils.ConstantUtils;
import cn.app.android.sichuanxiaoke.com.xiaokeapp.utils.PayListener;
import cn.app.android.sichuanxiaoke.com.xiaokeapp.utils.PaySDK;
import cn.app.android.sichuanxiaoke.com.xiaokeapp.utils.PaySDKFactory;

public class PayActivity extends AppCompatActivity {

    private String price;
    @BindView(R.id.trade_price)
    TextView mprice;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pay);
        ButterKnife.bind(this);
        initUI();
        //BandEvent();

    }

    private void BandEvent() {
        PaySDK paySDK = PaySDKFactory.getInstance(this);
        paySDK.setPayListener(new PayListener() {
            @Override
            public void onSuccess(Map<String, Object> message) {
                Log.i("paySDK","支付成功信息--onSuccess---"+message);
            }

            @Override
            public void onFail(String message) {
                Log.i("paySDK","支付失败信息--onFail---"+message);
            }
        });
    }


    private void initUI() {
        Intent intent = getIntent();
        price = intent.getStringExtra("price");
        mprice.setText(price+"  RMB");
        //mprice.setText(0.01+"  RMB");

    }
    @OnClick(R.id.GoPay)
    public void OnClick(View view){

       // Intent intent = new Intent(this, GoPayAppActivity.class);
        //intent.putExtra("moneyNumber",price);
       // intent.putExtra("moneyNumber",0.01+"");
       // startActivity(intent);

        initView();



    }
    private void initView() {
        //Intent intent = getIntent();
        //String moneyNumber = intent.getStringExtra("moneyNumber");
        /**
         *因为兴业银行的交易是以“分” 为单位，我们输入的是以“元为单位” 所以将元转换成分
         */
        float i = Float.valueOf(price);
        final int newmoneyNumber= (int) (i*100);
        new Thread(new Runnable() {
            @RequiresApi(api = Build.VERSION_CODES.KITKAT)
            @Override
            public void run() {
                JSONObject data = new JSONObject();
                try {
                    data.put("appid", "1100011000");
                    data.put("phone", "40WX4I0M01");
                    data.put("amount", newmoneyNumber);
                    data.put("mchnt_no", "309044353310001");
                    data.put("trans_type", "01");
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                OkGo.<String>post(ConstantUtils.CREATE_ORDER_URL)
                        .upJson(data.toString())
                        .execute(new StringCallback() {
                            @Override
                            public void onSuccess(Response<String> response) {
                                String s = response.toString();
                                String body = response.body();
                                try {
                                    JSONObject object = new JSONObject(body);
                                    String orderId = object.optString("order_id");
                                    toPayApp(orderId);
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                                Log.i("TAG", "Success---" + body);
                            }

                            @Override
                            public void onError(Response<String> response) {

                                Log.e("TAG", response.toString());

                                response.getException().printStackTrace();
                            }
                        });

            }
        }).start();
    }

    private void toPayApp(String orderid) {
        PaySDK paysdk = PaySDKFactory.getInstance(this);
        paysdk.setPayListener(new PayListener() {

            @Override
            public void onSuccess(Map<String, Object> message) {
                // TODO Auto-generated method stub
                Log.i("TAG", "支付完成...返回成功信息 === ppppp " + message);
                if (message.get("resCode") != null)
                    Toast.makeText(PayActivity.this, (String) message.get("resCode"), Toast.LENGTH_LONG).show();

               // MainActivity.this.finish();
//                requestState();
            }

            @Override
            public void onFail(String message) {
                // TODO Auto-generated method stub
                Log.i("TAG", "支付完成...返回失败信息 = " + message);
                Toast.makeText(PayActivity.this, message, Toast.LENGTH_LONG).show();

            }

        });
        if (true) {
            if (orderid != null) {
                //Map<String, Object> sendData = new HashMap<String, Object>();
                Map<String, Object> map = new HashMap();
                map.put("mchntNo", "309044353310001");
                map.put("phone", "40WX4I0M01");
                map.put("opeNum", "01");
                map.put("terminalNo", "43003726");
                map.put("mchntName", "hahah");
                map.put("transType", "cardPay");
                map.put("transactionId", orderid);
                // sendData.put("amount", amount);
                Log.i("TAG", "往支付app发送的数据data = " + map);
                paysdk.toPayApp(PayActivity.this, "cc.rengu.android.pay.ipps",
                        "cc.rengu.android.ui.activity.CollectionMoney", map);
            } else {

                Toast.makeText(this, "获取订单号失败！", Toast.LENGTH_LONG).show();
            }

        } else {

            Toast.makeText(this, "设置终端账号！", Toast.LENGTH_LONG).show();
        }

    }
}
